package com.jiezuo.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.jiezuo.auth.entity.*;
import com.jiezuo.auth.service.RolePermissionService;
import com.jiezuo.auth.service.RoleService;
import com.jiezuo.common.annotation.RepeatSubmit;
import com.jiezuo.common.base.BaseController;
import com.jiezuo.common.constant.SysConstant;
import com.jiezuo.common.enums.ResultEnums;
import com.jiezuo.common.util.*;
import com.jiezuo.common.vo.PageVo;
import com.jiezuo.common.vo.Result;




import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author 王帅
 * @Date 2020/7/29 10:15
 * @Version 1.0
 */
@Slf4j
@RequestMapping("/role")
@RestController
@Transactional
//角色(岗位)管理", tags = {"角色(岗位)管理接口"})
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    private RolePermissionService rolePermissionService;

    //查询带分页列表", notes = "查询带分页列表")
    @GetMapping("/findPage")
    public Result<IPage<Role>> findPage(@ModelAttribute PageVo pageVo, @ModelAttribute Role role) {
        IPage<Role> page = roleService.findPage(pageVo, role);
        return new ResultUtil<IPage<Role>>().setData(page);
    }

    //查询不带分页列表", notes = "查询不带分页列表")
    @GetMapping("/findList")
    public Result<List<Role>> findList(@ModelAttribute Role role) {
        List<Role> list = roleService.findList(role);
        return new ResultUtil<List<Role>>().setData(list);
    }

    @RepeatSubmit
    //添加", notes = "添加角色信息")
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        //非空校验
        VerifyUtil.notEmpty("角色状态",role.getStatus());
        VerifyUtil.notEmpty("角色名称",role.getRole_name());
        roleService.save(role);
        // 清理redis数据
        Set<String> keys = redisTemplate.keys(SysConstant.REDIS_DATA_AUTHORITY+"*");
        Long delete = redisTemplate.delete(keys);
        return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    //修改", notes = "修改角色信息")
    @PostMapping("/update")
    public Result update(@RequestBody Role role) {
        //非空校验
        VerifyUtil.notEmpty("数据序号",role.getId());
        VerifyUtil.notEmpty("角色状态",role.getStatus());
        VerifyUtil.notEmpty("角色名称",role.getRole_name());
        // 编辑部门角色
        if(role.getDepts() != null){
            List<String> departIds = role.getDepts().stream().map(Depart::getId).collect(Collectors.toList());
            role.setRole_depart(String.join(",", departIds));
        }
        roleService.updateById(role);
        // 清理redis数据
        Set<String> keys = redisTemplate.keys(SysConstant.REDIS_DATA_AUTHORITY+"*");
        Long delete = redisTemplate.delete(keys);
        return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    //删除", notes = "删除角色信息")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        Role role = new Role();
        role.setId(id);
        role.setIs_delete("1");
        roleService.updateById(role);
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        rolePermissionService.remove(queryWrapper);
        // 清理redis数据
        Set<String> keys = redisTemplate.keys(SysConstant.REDIS_DATA_AUTHORITY+"*");
        Long delete = redisTemplate.delete(keys);
        return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    //批量删除", notes = "批量删除")
    @PostMapping("/deleteBatch")
    public Result delete(@RequestBody String[] ids) {
        List<Role> list = new ArrayList<>();
        for(String id:ids){
            Role role = roleService.getById(id);
            if(role != null){
                role.setIs_delete("1");
                list.add(role);
            }
        }
        if(list.size()>0){
            roleService.updateBatchById(list);
        }
        // 清理redis数据
        Set<String> keys = redisTemplate.keys(SysConstant.REDIS_DATA_AUTHORITY+"*");
        Long delete = redisTemplate.delete(keys);
        return new ResultUtil().setSuccessMsg(ResultEnums.OK.getCode());
    }

    //是否启用", notes = "是否启用")
    @PostMapping("/isEnabled")
    public Result isEnabled(@RequestBody Role role) {
        boolean boo = roleService.updateById(role);
        // 清理redis数据
        Set<String> keys = redisTemplate.keys(SysConstant.REDIS_DATA_AUTHORITY+"*");
        Long delete = redisTemplate.delete(keys);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

}
